#include <54func.h>
//目标：测试主线程与子线程是共享同一个打开的文件
void *threadFunc(void *arg)
{
    int *pfd =(int *)arg;
    char buf[4096] = {0};
    //因为子线程与主线程共享文件，那么文件偏移量也是共享，主线程写完文件，文件偏移量位于末尾
    lseek(*pfd,0,SEEK_SET);
    read(*pfd,buf,sizeof(buf));
    printf("I am child!buf = %s\n",buf);
    return 0;
}
int main(int argc,char *argv[])
{
    // ./2_homework_openfile fil1
    ARGS_CHECK(argc,2);
    int fd = open(argv[1],O_RDWR);
    ERROR_CHECK(fd,-1,"open");
    write(fd,"hello",5);

    pthread_t tid;
    pthread_create(&tid,NULL,threadFunc,&fd);
    sleep(2);//确保子线程读取文件结束！
    return 0;
}
